package cn.com.haopy.yue.ai.wx.service;


import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskCheckIndex;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskIllnessCheckIndex;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapers;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapersIndex;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskCheckIndexService;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskIllnessCheckIndexService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author keel
 * @date 2022/4/13
 */
@Service
@Slf4j
public class WxCommonCheckIndexService {


    @Autowired
    AIRiskIllnessCheckIndexService riskIllnessCheckIndexService;

    @Autowired
    AIRiskCheckIndexService riskCheckIndexService;


    /**
     * 昊邦体检数据创建
     *
     * @param haopyReporData
     * @param papers
     * @return
     * @throws Exception
     */
    public List<AIRiskPapersIndex> getPaperIndexByHaopy(JSONObject haopyReporData, AIRiskPapers papers) throws Exception {


        List<AIRiskPapersIndex> paperIndexSaveList = new ArrayList<>();

        //判断标准一至的
        List<AIRiskCheckIndex> checkIndexList = riskCheckIndexService.getJudgeNormalList();

        for (AIRiskCheckIndex checkIndexItem : checkIndexList) {
            if (Objects.nonNull(checkIndexItem)) {
                AIRiskPapersIndex papersIndexItem = new AIRiskPapersIndex();
                papersIndexItem.setPaperId(papers.getId());
                papersIndexItem.setIndexId(checkIndexItem.getId());
                papersIndexItem.setIndexName(checkIndexItem.getCheckIndexName());

                papersIndexItem.setNormalVal(checkIndexItem.getNormalVal());
                //根据体检报告获取指标值
                if (checkIndexItem.getCheckIndexName().equals("性别")) {
                    papersIndexItem.setIndexValue(papers.getSex());
                } else {
                    papersIndexItem.setIndexValue(this.getHaopyCheckReportIndex(haopyReporData, checkIndexItem));
                }

                //riskPapersIndexService.save(papersIndexItem);
                paperIndexSaveList.add(papersIndexItem);
            }

        }

        //判断标准不一致的，比如年龄
        List<AIRiskCheckIndex> checkIndexJudgeIllnessList = riskCheckIndexService.getJudgeIllnessList();
        for (AIRiskCheckIndex checkIndexItem : checkIndexJudgeIllnessList) {
            List<AIRiskIllnessCheckIndex> illnessCheckIndexList = riskIllnessCheckIndexService.getByIndexId(checkIndexItem.getId());
            for (AIRiskIllnessCheckIndex illnessCheckIndexItem : illnessCheckIndexList) {
                AIRiskPapersIndex papersIndexItem = new AIRiskPapersIndex();
                papersIndexItem.setPaperId(papers.getId());
                papersIndexItem.setIndexId(checkIndexItem.getId());
                papersIndexItem.setIndexName(checkIndexItem.getCheckIndexName());
                //疾病ID
                papersIndexItem.setIllnessId(illnessCheckIndexItem.getIllnessId());

                papersIndexItem.setNormalVal(illnessCheckIndexItem.getNormalVal());

                if (checkIndexItem.getCheckIndexName().equals("年龄")) {
                    //根据体检报告获取指标值
                    papersIndexItem.setIndexValue(papers.getAge() + " 岁");
                } else {
                    //根据体检报告获取指标值
                    papersIndexItem.setIndexValue(this.getHaopyCheckReportIndex(haopyReporData, checkIndexItem));
                }

                paperIndexSaveList.add(papersIndexItem);

            }
        }

        return paperIndexSaveList;

    }


    /**
     * 从体检报告中获取对应的指标值(昊邦)
     *
     * @param indexItem
     * @return
     */
    private String getHaopyCheckReportIndex(JSONObject data, AIRiskCheckIndex indexItem) {
        try {
            /*
            ClassPathResource resource = new ClassPathResource("data/check_report.json");
            byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
            String str = new String(bytes, StandardCharsets.UTF_8);

            JSONObject data = JSONObject.parseObject(str);

             */

            if (StringUtils.hasText(data.getString(indexItem.getCheckIndexName()))) {
                return data.getString(indexItem.getCheckIndexName());
            }
        } catch (Exception e) {
            log.error(e.toString());
        }

        return "";
    }


    /******************************************************** pdf 数据解析创建 ********************************************* /
     /******************************************************** pdf 数据解析创建 ********************************************* /


     /**
     * pdf 数据解析 创建
     *
     * @param array
     * @param papers
     * @return
     * @throws Exception
     */
    public List<AIRiskPapersIndex> getPaperIndexByPdfJson(JSONArray array, AIRiskPapers papers) throws Exception {

        List<AIRiskPapersIndex> paperIndexSaveList = new ArrayList<>();
        //判断标准一至的
        List<AIRiskCheckIndex> checkIndexList = riskCheckIndexService.getJudgeNormalList();

        for (AIRiskCheckIndex checkIndexItem : checkIndexList) {
            if (Objects.nonNull(checkIndexItem)) {
                AIRiskPapersIndex papersIndexItem = new AIRiskPapersIndex();
                papersIndexItem.setPaperId(papers.getId());
                papersIndexItem.setIndexId(checkIndexItem.getId());
                papersIndexItem.setIndexName(checkIndexItem.getCheckIndexName());

                //papersIndexItem.setNormalVal(checkIndexItem.getNormalVal());
                //改成从体检报告里面取
                papersIndexItem.setNormalVal(this.getCheckReportPdfIndexNormal(array, checkIndexItem));
                //根据体检报告获取指标值
                if (checkIndexItem.getCheckIndexName().equals("性别")) {
                    papersIndexItem.setIndexValue(papers.getSex());
                } else {
                    papersIndexItem.setIndexValue(this.getCheckReportPdfIndex(array, checkIndexItem));
                }

                //riskPapersIndexService.save(papersIndexItem);
                paperIndexSaveList.add(papersIndexItem);
            }

        }

        //判断标准不一致的，比如年龄
        List<AIRiskCheckIndex> checkIndexJudgeIllnessList = riskCheckIndexService.getJudgeIllnessList();
        for (AIRiskCheckIndex checkIndexItem : checkIndexJudgeIllnessList) {
            List<AIRiskIllnessCheckIndex> illnessCheckIndexList = riskIllnessCheckIndexService.getByIndexId(checkIndexItem.getId());
            for (AIRiskIllnessCheckIndex illnessCheckIndexItem : illnessCheckIndexList) {
                AIRiskPapersIndex papersIndexItem = new AIRiskPapersIndex();
                papersIndexItem.setPaperId(papers.getId());
                papersIndexItem.setIndexId(checkIndexItem.getId());
                papersIndexItem.setIndexName(checkIndexItem.getCheckIndexName());
                //疾病ID
                papersIndexItem.setIllnessId(illnessCheckIndexItem.getIllnessId());

                //papersIndexItem.setNormalVal(illnessCheckIndexItem.getNormalVal());
                //改成从体检报告里面取
                papersIndexItem.setNormalVal(this.getCheckReportPdfIndexNormal(array, checkIndexItem));


                if (checkIndexItem.getCheckIndexName().equals("年龄")) {
                    //根据体检报告获取指标值
                    papersIndexItem.setIndexValue(papers.getAge() + " 岁");
                } else {
                    //根据体检报告获取指标值
                    papersIndexItem.setIndexValue(this.getCheckReportPdfIndex(array, checkIndexItem));
                }

                paperIndexSaveList.add(papersIndexItem);

            }
        }

        return paperIndexSaveList;
    }


    /**
     * 从体检报告中获取对应的指标值------pdf---提取返回
     *
     * @param indexItem
     * @return
     */
    private String getCheckReportPdfIndex(JSONArray array, AIRiskCheckIndex indexItem) {
        try {
            for (Integer i = 0; i < array.size(); i++) {
                JSONObject checkItemJson = array.getJSONObject(i);
                if (StringUtils.hasText(checkItemJson.getString("检查项目")) && checkItemJson.getString("检查项目").equals(indexItem.getCheckIndexName())) {
                    return checkItemJson.getString("测量结果");
                }
                if (StringUtils.hasText(checkItemJson.getString("标准项目")) && checkItemJson.getString("标准项目").equals(indexItem.getCheckIndexName())) {
                    return checkItemJson.getString("测量结果");
                }
            }

        } catch (Exception e) {
            log.error(e.toString());
        }

        return "";
    }

    /**
     * 从体检报告中获取对应的指标值------pdf---提取返回 ---参考区间
     *
     * @param indexItem
     * @return
     */
    private String getCheckReportPdfIndexNormal(JSONArray array, AIRiskCheckIndex indexItem) {
        try {
            for (Integer i = 0; i < array.size(); i++) {
                JSONObject checkItemJson = array.getJSONObject(i);
                if (StringUtils.hasText(checkItemJson.getString("检查项目")) && checkItemJson.getString("检查项目").equals(indexItem.getCheckIndexName())) {
                    return checkItemJson.getString("正常参考值");
                }
                if (StringUtils.hasText(checkItemJson.getString("标准项目")) && checkItemJson.getString("标准项目").equals(indexItem.getCheckIndexName())) {
                    return checkItemJson.getString("正常参考值");
                }
            }

        } catch (Exception e) {
            log.error(e.toString());
        }

        return "";
    }

}
